<?php
/**
 * Created by PhpStorm.
 * User: JM003
 * Date: 2020/9/17
 * Time: 10:17
 */

namespace app\admin\model;


use think\Db;
use think\Model;

class OrganizationModel extends Model
{
    protected $name = 'sys_dept';
    protected $autoWriteTimestamp = true;//时间自动更新 create_time ,update_time

    protected $field = true;

    public function getDeptlist(){
        $list = $this->where("isdelete=0")->field("*,name as title,id as id")->select();

        if($list){
            foreach ($list as $k=>$v){
                $new[] = $v->data;
            }
        }
        $tree = $this->getTree($new,0);

        return $tree;
    }

    /**
     * @param $data
     * @param $pId
     * @return array
     * 根据部门数据组成树结构
     */
    function getTree($data, $pId)
    {
        $tree = array();
        foreach($data as $k => $v)
        {

            if($v['parentid'] == $pId)
            {        //父亲找到儿子

                $v['children'] = $this->getTree($data, $v['id']);
                $tree[] = $v;
                unset($data[$k]);
            }
        }
        return $tree;
    }

    /**
     * 关联用户表
     */
    public function getStaffData(){
        return $this->belongsTo("SysUserModel","dep_id",'id');
    }

    /**
     * 获取员工列表
     * @param $param
     * @return array
     */
    public function getDeptStaffList($param){
        $id = $param['id'];
        $type = $param['type'];

        $param['table'] = 'jmzhfs_sys_user';
        $param['join'] = [
            ['jmzhfs_sys_dept b','a.dep_id=b.id']
        ];
        $param['field'] = 'b.name as deptname,b.id as dep_id,b.companyid,a.id,a.is_executive,a.status,a.imgurl,a.username,a.name';

        if($type==1){
            $param['where'] = " a.isdelete=0 and b.companyid=$id";
        }else{
            $param['where'] = " a.isdelete=0 and a.dep_id=$id";
        }

//
//        //dump($param);die;
        $result = Db::table($param['table'])
            ->alias('a')
            ->join($param['join'])
            ->field($param['field'])
            ->where($param['where'] )
            ->page($param['page'])
            ->limit($param['limit'])
            ->select();

        $count = Db::table($param['table'])
            ->alias('a')
            ->join($param['join'])
            ->field($param['field'])
            ->where($param['where'] )
            ->count();
        if($result){
            $ret = [
                'msg'=>'获取数据成功',
                'code'=>0,
                'count'=>$count,
                'data'=>$result,
            ];
        }else{
            $ret = [
                'msg'=>'获取数据为空',
                'code'=>0,
                'count'=>0,
                'data'=>'',
            ];
        }

        return $ret;

    }

    public function addUpdataDept($param){

        $table = "jmzhfs_sys_dept";
        $type = $param['type'];
        $id = $param['id'];
        $opert = $param['opert'];
        if($opert!='del'){
            $editdepaname = $param['editdepaname'];
            $where = "isdelete=0";
            $where.= " and name='$editdepaname'";

            $old = $this->where($where)->find();
        }else{
            $old = '';
        }


        //echo $this->getLastsql();die;

        if($opert=='add'){

            if($old){
                $ret['code'] = -1;
                $ret['msg'] ='新增部门已经存在，请重新添加';
                $ret['data'] = '';
                return $ret;
            }
            $dept['name'] = $param['editdepaname'];
            $dept['companyid'] = $param['companyid'];

            $dept['isdelete'] = 0;
            if($param['parentid']==0 && $type==1){
                $dept['parentid'] = $id;
                $dept['type'] = 2;
                $dept['depth'] = ($param['depth'])+1;
            }else{
                $dept['parentid'] = $id;
                $dept['type'] = $type;
                $dept['depth'] = ($param['depth'])+1;
            }

            $result = $this->allowField(true)->save($dept);
            if($result){
                $ret['msg'] ='新增部门成功';
                $ret['data'] = $this->getDeptList();

            }else{
                $ret['code'] = -1;
                $ret['msg'] ='新增部门失败';
            }
        }else{
            //$updata['updata_time'] = date('Y-m-d H:i:s');
            if($opert=='update'){

                if($old){
                    $ret['code'] = -1;
                    $ret['msg'] ='修改的部门已经存在，请重新修改';
                    return $ret;
                }

                if($old[0]['name'] ==$param['editdepaname']){
                    $ret['msg'] ='修改部门成功';
                    $ret['code'] = 1;
                    return $ret;
                }

                $updata['id'] = $id;
                $updata['name'] = $param['editdepaname'];
                $result = $this->allowField(true)->save($updata,$updata['id'] = $id);
                if(!$result){
                    $ret['code'] = -1;
                    $ret['msg'] ='修改部门失败';
                }else{
                    $ret['msg'] ='修改部门成功';
                }
            }else{
                $updata['id'] = $id;
                $updata['isdelete'] = 1;

                $result = $this->allowField(true)->save($updata,$updata['id']);
                if(!$result){
                    $ret['code'] = -1;
                    $ret['msg'] ='删除部门失败';
                }else{
                    $ret['msg'] ='删除部门成功';
                }
            }
        }

        return $ret;
    }

    /**
     * 新增或者更新员工信息
     * @return array
     */
    public function addUpdataStaff($param){

        $model = new SysUserModel();

        $type = $param['type'];
        
        if($type=='edit'){
            $result = $model->allowField(true)->save($param,['id'=>$param['id']]);
        }elseif($type=='del'){
            $param['isdelete'] = 1;
            $result = $model->allowField(true)->save($param,['id'=>$param['id']]);
        }else{
            $param['isdelete'] = 0;
            $result = $model->allowField(true)->save($param);
        }
        //echo $model->getLastSql();die;
        $ret['data'] = '';
        if($result){
            $ret['code'] = 1;
            if($type=='edit'){
                $ret['msg'] = '修改成功';
            }elseif($type=='del'){
                $ret['msg'] = '删除成功';
            }else{
                $ret['msg'] = '添加成功';
            }
        }else{
            $ret['code'] = -1;
            if($type=='edit'){
                $ret['msg'] = '修改失败';
            }elseif($type=='del'){
                $ret['msg'] = '删除失败';
            }else{
                $ret['msg'] = '添加失败';
            }

        }
        return $ret;

    }



}